// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SimpleTextFormatter.java

package org.quickserver.util.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
import org.quickserver.util.MyString;

public class SimpleTextFormatter extends Formatter
{

    public SimpleTextFormatter()
    {
        logThreadName = true;
    }

    public String format(LogRecord record)
    {
        Date date = new Date();
        date.setTime(record.getMillis());
        StringBuilder sb = new StringBuilder();
        sb.append(df.format(date));
        sb.append(" [");
        sb.append(MyString.alignLeft(record.getLevel().getLocalizedName(), 7));
        sb.append("]");
        sb.append(" [").append(record.getThreadID()).append("] - ");
        if(record.getSourceClassName() != null)
            sb.append(record.getSourceClassName());
        else
            sb.append(record.getLoggerName());
        if(record.getSourceMethodName() != null)
        {
            sb.append('.');
            sb.append(record.getSourceMethodName());
        }
        if(isLogThreadName())
        {
            sb.append(" - [");
            sb.append(Thread.currentThread().getName());
            sb.append("]");
        }
        sb.append(" - ");
        sb.append(formatMessage(record));
        if(record.getThrown() != null)
        {
            sb.append(lineSeparator);
            sb.append("[StackTrace: ");
            try
            {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            }
            catch(Exception ex)
            {
                sb.append(record.getThrown().toString());
            }
            sb.append(']');
        }
        sb.append(lineSeparator);
        return sb.toString();
    }

    public boolean isLogThreadName()
    {
        return logThreadName;
    }

    public void setLogThreadName(boolean logThreadName)
    {
        this.logThreadName = logThreadName;
    }

    private boolean logThreadName;
    private final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
    private final String lineSeparator = System.getProperty("line.separator");
}
